package com.doradance.app.appactivity.message;


import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.doradance.R;
import com.doradance.account.ui.LoginActivity;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.MyApplication;
import com.doradance.app.home.ClassEvent;
import com.doradance.app.mine.SetActivity;
import com.doradance.entity.refreshEntity;
import com.doradance.messageDao.Info;
import com.doradance.messageDao.InfoDao;
import com.doradance.messageDao.Message;
import com.doradance.messageDao.MessageDao;
import com.pachong.android.baseuicomponent.LoadState;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collections;
import java.util.List;

import static com.doradance.account.AccountManager.getTokenInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class MessageListFragment extends SwipeListFragment {
    MessageDao messageDao;
    MessageListAdapter mAdapter;
    public MessageListFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        messageDao = MyApplication.getDaoSession(MyApplication.getAppContext()).getMessageDao();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        setPullToRefreshEnable(false);
        startLoading();
        clearDecorations();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        mAdapter = new MessageListAdapter(getContext());
        return mAdapter;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        List<Message> list = messageDao.queryBuilder().build().list();

        Long l = new Long((long)position+1);
        for(int i=0;i<list.size();i++){
            if(l.equals(list.get(i).getId())){
                Message message = new Message(list.get(list.size()-i-1).getId(),1 + "", list.get(list.size()-i-1).getData());
                messageDao.update(message);
            }
        }
        Message o = (Message) getAdapter().getData().get(position);
        getData().clear();
        startLoading();
        MessageContentActivity.start(getActivity(),o.getData());
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long itemId) {
        //退出登录
        final AlertDialogFragment singOutDialogFrag = new AlertDialogFragment();
        singOutDialogFrag.setMessage("是否确定删除该条消息?")
                .setNegativeButton("取消", true, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        singOutDialogFrag.dismiss();
                    }
                })
                .setPositiveButton("确定", false, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<Message> list = messageDao.queryBuilder().build().list();
                        Long l = new Long((long)position+1);
                        for(int i=0;i<list.size();i++){
                            if(l.equals(list.get(i).getId())){
                                Message message = new Message(list.get(list.size()-i-1).getId(),1 + "", list.get(list.size()-i-1).getData());
                                messageDao.delete(message);
                                getData().clear();
                                startLoading();
                            }
                        }
                        singOutDialogFrag.dismiss();
                    }
                }).show(getFragmentManager());

        return super.onItemLongClick(arg0, arg1, position, itemId);
    }

    @Override
    public void onStartLoading() {
        List<Message> list = messageDao.queryBuilder().build().list();
        Collections.reverse(list);
        getAdapter().getData().addAll(list);
        getAdapter().notifyDataSetChanged();
        changeLoadState(LoadState.FINISH);

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(refreshEntity refreshEvent){
        getData().clear();
        startLoading();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
